'use strict';

const conn = require('../../libs/Conn');
const _ = require('underscore');
const moment = require('moment');
module.exports = async function (params) {
    let buildingUseSql ='';
    let regionSql = '';
    let yearSql = '';
    let orderBySql = ``;
    if(params.sort&&params.dir){
        switch (params.sort) {
            case 'engineering_name':
                orderBySql = `ORDER BY ef.engineering_name ${params.dir}`;
                break;
            case 'structure_name':
                orderBySql = `ORDER BY sf.structure_name ${params.dir}`;
                break;
            case 'region_name':
                orderBySql = `ORDER BY rf.region_name ${params.dir}`;
                break;
            case 'aboveGround_floor':
                orderBySql = `ORDER BY eif.aboveGround_floor ${params.dir}`;
                break;
            case 'underGround_floor':
                orderBySql = `ORDER BY eif.underGround_floor ${params.dir}`;
                break;
            case 'engineering_area':
                orderBySql = `ORDER BY eif.engineering_area ${params.dir}`;
                break;
            case 'engineering_address':
                orderBySql = `ORDER BY eif.engineering_address ${params.dir}`;
                break;
            case 'status':
                orderBySql = `ORDER BY ef.status ${params.dir}`;
                break;
        }
    }
    /*分页*/
     let pageSql = `LIMIT ${params.pageNum>1?((params.pageNum-1)*10):0},10;SELECT FOUND_ROWS() AS count`;

    /*年份条件*/
    if(params.nd){
        let yearBegin = moment(params.nd).format('YYYY-01-01');
        let yearEnd = moment(params.nd).format('YYYY-12-31');
        yearSql = `AND (ef.startDate BETWEEN '${yearBegin}' AND '${yearEnd}') `;
    }

    /*建筑用途条件*/
    if(params.jzyt){
        if(params.jzyt!=='0'){
            buildingUseSql = `AND ef.building_id = '${params.jzyt}'`;
        }
    }

    /*区域条件*/
    let userSql = ``;
    if(params.qy){
        if(params.qy!=='0') {
            regionSql = `AND ef.region_id = '${params.qy}'`;
        }
    }else{
        if(params.is_manager !==1){
            userSql = `AND ef.engineering_id IN (               SELECT
                                                                engineering_id
                                                            FROM
                                                                engineering_file
                                                            WHERE
                                                                principal_id = '${params.user_id}'
                                                            AND delete_sign = 1
                                                            UNION
                                                                SELECT
                                                                    efa.engineering_id
                                                                FROM
                                                                    engineering_user_middle eum
                                                                INNER JOIN engineering_file efa ON efa.engineering_id = eum.engineering_id
                                                                AND efa.delete_sign = 1
                                                                WHERE
                                                                    eum.user_id = '${params.user_id}'
                                                            ) `;
        }
    }


    let sql = `SELECT
                    SQL_CALC_FOUND_ROWS
                    ef.engineering_name,
                    sf.structure_name,
                    rf.region_name,
                    eif.aboveGround_floor,
                    eif.underGround_floor,
                    eif.engineering_area,
                    eif.engineering_address,
                    cue.unit_name AS construction_unit_employer_name,
                    dwu.unit_name AS drawing_unit_name,
                    svu.unit_name AS survey_unit_name,
                    cuc.unit_name AS construction_unit_contractor_name,
                    spu.unit_name AS supervisory_unit_name,
                    dsu.unit_name AS design_unit_name,
                    ef.status
                FROM
                    engineering_file ef
                LEFT JOIN region_file rf ON ef.region_id = rf.region_id   
                LEFT JOIN structure_file sf ON ef.structure_id = sf.structure_id 
                LEFT JOIN engineering_information_file eif ON ef.engineering_id = eif.engineering_id 
                LEFT JOIN unit_file cue ON eif.construction_unit_employer_id = cue.unit_id 
                LEFT JOIN unit_file dsu ON eif.design_unit_id = dsu.unit_id 
                LEFT JOIN unit_file cuc ON eif.construction_unit_contractor_id = cuc.unit_id 
                LEFT JOIN unit_file spu ON eif.supervisory_unit_id = spu.unit_id 
                LEFT JOIN unit_file svu ON eif.survey_unit_id = svu.unit_id 
                LEFT JOIN unit_file dwu ON eif.drawing_unit_id = dwu.unit_id 
                WHERE
                    ef.delete_sign = 1 ${userSql} ${yearSql} ${buildingUseSql} ${regionSql} ${orderBySql} ${pageSql}`;

    let rec = await conn(sql);
    _.each(rec[0],function (data) {
        switch (data.status) {
            case 1:
                data.status = '在建';
                break;
            case 2:
                data.status = '竣工未验收';
                break;
            case 3:
                data.status = '已验收未备案';
                break;
            case 4:
                data.status = '已验收且备案';
                break;
        }
    });
    let pageNum = Math.ceil(rec[1][0].count/10);
    if(rec[0].length>0){
        rec[0][0].dataTotal = rec[1][0].count;
        rec[0][0].pageTotal = pageNum;
        rec[0][0].pageNum = params.pageNum;
    }
    return {success:true,data:rec[0]}
};